﻿using System;
using System.Collections.Generic;
using System.Text;
using Lizk.SimpleHUD;
using SharpMedia.Controls;

namespace SharpMedia.Pages
{
    public class VideoContextPage: MediaPage
    {
        public HUDVideo2 Video { get { return VOptions.Video; } set { VOptions.Video = value; } }
        private HUDVideoOptions vOptions;

        public HUDVideoOptions VOptions
        {
            get { return vOptions; }
        }

        public bool IsDVD(string file) { return file.ToLower().EndsWith(".ifo");}

        private SliderContextBarItem ctxBrightness, ctxContrast, ctxSaturation, ctxHue;
        private AudioTrackSelector ctxAudioTracks;
        private SubtitleTrackSelector ctxSubtitles;
        private ZoomSelector ctxZoom;
        private YesNoSelector ctxContinue;

        public bool StopBetweenParts
        {
            get 
            {
                return ctxContinue.Enabled;
            }
            set 
            {
                ctxContinue.Enabled = value;
            }
        }

        private TimedTrigger<string> ctxTrigger = new TimedTrigger<string>(null, new TimeSpan(0, 0, 0, 2, 500));

        public VideoContextPage(HUDPage parent, HUDVideo2 video)
            : base(parent)
        {
            this.Anchor = Anchor.All;
            
            vOptions = new HUDVideoOptions(video);
            vOptions.Size = Size;
            vOptions.Anchor = Anchor.All;
            vOptions.Alpha = 255;
            Add(vOptions);


            ctxAudioTracks = new AudioTrackSelector(vOptions, "Audio Tracks", "movies.png");
            ctxSubtitles = new SubtitleTrackSelector(vOptions, "Subtitles", "anime.png");
            ctxBrightness = new BrightnessSelector(vOptions, "Brightness", "movies.png");
            ctxContrast = new ContrastSelector(vOptions, "Contrast", "movies.png");
            ctxSaturation = new SaturationSelector(vOptions, "Saturation", "movies.png");
            ctxHue = new HueSelector(vOptions, "Hue", "movies.png");
            ctxZoom = new ZoomSelector(vOptions, "Zoom", "movies.png");
            ctxContinue = new YesNoSelector(vOptions, "Show StopMenu after Episode", "pb_stop140.png");
            ctxContinue.Enabled = true;
            vOptions.AddItem(ctxZoom);
            if(!IsDVD(video.SourceUrl))
                vOptions.AddItem(ctxContinue);

            ctxTrigger.TimeOut += new EventHandler(ctxTrigger_TimeOut);
            ctxTrigger.FirstTrigger += new EventHandler(ctxTrigger_FirstTrigger);

        }

        public void UpdateItems()
        {
            this.Size = parent.Size;
            
            if (Video.Subtitles.Count > 1)
            {
                //lblSubtitle.Text = video.Subtitles[(video.ActiveSubtitle + subOffset) % video.Subtitles.Count].name + " (" + ((video.ActiveSubtitle + subOffset) % video.Subtitles.Count + 1) + "/" + video.Subtitles.Count + ") :Subtitle";
                if (!vOptions.Contains(ctxSubtitles))
                {
                    vOptions.AddItem(ctxSubtitles);
                }
//                else
//                    ctxSubtitles.Reapply();
                ctxSubtitles.UpdateTracks();
            }
            else
            {
                vOptions.Remove(ctxSubtitles);
            }
            if (Video.Audiotracks.Count > 1)
            {
                //lblAudio.Text = "Audio: (" + ((video.ActiveAudiotrack + audioTrackOffset) % video.Audiotracks.Count + 1) + "/" + video.Audiotracks.Count + ") " + video.Audiotracks[(video.ActiveAudiotrack + audioTrackOffset) % video.Audiotracks.Count].name;

                if (!vOptions.Contains(ctxAudioTracks))
                {
                    vOptions.AddItem(ctxAudioTracks);
                }
//                else
//                    ctxAudioTracks.Reapply();
                ctxAudioTracks.UpdateTracks();
            }
            else
            {
                vOptions.Remove(ctxAudioTracks);
            }

            if ((Video.ProcAmpCaps & HUDVideo2.ProcAmp.Brightness) == HUDVideo2.ProcAmp.Brightness)
            {
                if (!vOptions.Contains(ctxBrightness))
                {
                    vOptions.AddItem(ctxBrightness);
                    ctxBrightness.Value = Video.Brightness;
                }
                else
                    ctxBrightness.Reapply();

            }
            else
            {
                vOptions.Remove(ctxBrightness);
            }
            if ((Video.ProcAmpCaps & HUDVideo2.ProcAmp.Contrast) == HUDVideo2.ProcAmp.Contrast)
            {
                if (!vOptions.Contains(ctxContrast))
                {
                    vOptions.AddItem(ctxContrast);
                    ctxContrast.Value = Video.Contrast;
                }
                else
                    ctxContrast.Reapply();
            }
            else
            {
                vOptions.Remove(ctxContrast);
            }
            if ((Video.ProcAmpCaps & HUDVideo2.ProcAmp.Saturation) == HUDVideo2.ProcAmp.Saturation)
            {
                if (!vOptions.Contains(ctxSaturation))
                {
                    vOptions.AddItem(ctxSaturation);
                    ctxSaturation.Value = Video.Saturation;
                }
                else
                    ctxHue.Reapply();
            }
            else
            {
                vOptions.Remove(ctxSaturation);
            }
            if ((Video.ProcAmpCaps & HUDVideo2.ProcAmp.Hue) == HUDVideo2.ProcAmp.Hue)
            {
                if (!vOptions.Contains(ctxHue))
                {
                    vOptions.AddItem(ctxHue);
                    ctxHue.Value = Video.Hue;
                }
                else
                {
                    ctxHue.Reapply();
                }

            }
            else
            {
                vOptions.Remove(ctxHue);
            }
            ctxZoom.Reapply();
            
        }



        void ctxTrigger_FirstTrigger(object sender, EventArgs e)
        {
        }

        void ctxTrigger_TimeOut(object sender, EventArgs e)
        {
            if(SharpMedia.CurrentPage == this)
                SharpMedia.Back();
        }

        public override void Show()
        {
            Fade(255, 6f);
            ctxTrigger.Trigger();
        }

        public override void Close()
        {
            Fade(0, 6f);
        }

        public override void Update(TimeSpan ts)
        {
            ctxTrigger.Update();
            base.Update(ts);
        }

        public override void Command(Lizk.SimpleHUD.HUDCommands command, object tag)
        {
            switch (command)
            {
                case HUDCommands.Menu:
                case HUDCommands.Select:
                    ctxTrigger.Stop(true);
                    return;
                case HUDCommands.Up:
                    vOptions.Up();
                    ctxTrigger.Trigger();
                    return;
                case HUDCommands.Down:
                    vOptions.Down();
                    ctxTrigger.Trigger();
                    return;
                case HUDCommands.Left:
                    vOptions.SelectedIndex--;
                    ctxTrigger.Trigger();
                    return;
                case HUDCommands.Right:
                    vOptions.SelectedIndex++;
                    ctxTrigger.Trigger();
                    return;
                default:
                    if (parent != null)
                        parent.Command(command, tag);
                    break;
            }

        }
    }
}
